/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Form;

import data.Column;
import data.Table;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.plaf.TabbedPaneUI;
import javax.swing.plaf.basic.BasicButtonUI;
import javax.swing.table.TableColumn;

/**
 *
 * @author GREENWIND
 */
public final class TabbedTable extends JPanel {

    private final JTabbedPane pane;
    private final JLabel label;
    private final JButton button = new TabButton();
    private final JTable table = new JTable(50, 3);
    private String database;
    private String name;
    JScrollPane jsp_tb;

    public String tabName() {
        return name;
    }

    public String getDatabase() {
        return this.database;
    }

    public TabbedTable(String title, JTabbedPane pane, String Database) {
        super(new FlowLayout(FlowLayout.LEFT, 0, 0));
        if (pane == null) {
            throw new NullPointerException("TabbedPane is null");
        }
        this.pane = pane;
        database = Database;
        name = title;
        setOpaque(false);
        label = new JLabel(title);
        add(label);
        label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 2));
        add(button);
        setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
        InitTable();
        JTextArea wel = new JTextArea();
        wel.setEnabled(false);
        if (title.equals("Welcome")) {
            pane.add(wel);
        } else {
            pane.add(jsp_tb);
        }

    }

    

    public void InitTable() {
        table.getColumnModel().getColumn(1).setHeaderValue("Field Name");
        table.getColumnModel().getColumn(2).setHeaderValue("Data Type");
        table.getColumnModel().getColumn(0).setHeaderValue("Primary Key");
        table.getColumnModel().getColumn(0).setPreferredWidth(20);
        TableColumn typeColumn = table.getColumnModel().getColumn(2);
        JComboBox typedat = new JComboBox();
        typedat.addItem("Number");
        typedat.addItem("Text");
        typeColumn.setCellEditor(new DefaultCellEditor(typedat));
        TableColumn colPrimaryKey = table.getColumnModel().getColumn(0);
        JComboBox typeKey = new JComboBox();
        typeKey.addItem("Primary Key");
        typeKey.addItem("");
        colPrimaryKey.setCellEditor(new DefaultCellEditor(typeKey));
        jsp_tb = new JScrollPane(table);
        if(database != null){
        for (int i = 0; i < FormMain.ListDatabase.size(); i++) {
            if (FormMain.ListDatabase.get(i).GetName().equals(database)) {
                Table tb = FormMain.ListDatabase.get(i).getTable(name);
                if(tb == null){
                return;
                }
                if (!tb.getListCol().isEmpty()) {
                    ArrayList<Column> cols = tb.getListCol();
                    for (int j = 0; j < cols.size(); j++) {
                        if (cols.get(j).getIsPrimary()) {
                            table.getModel().setValueAt("Primary Key", j, 0);
                        }
                        table.getModel().setValueAt(cols.get(j).getName(), j, 1);

                        if (cols.get(j).getType() == 0) {
                            table.getModel().setValueAt("Number", j, 2);
                        } else {
                            table.getModel().setValueAt("Text", j, 2);
                        }


                    }
                }
            }
        }
        }
    }

    public ArrayList<Column> getDataCol() {
        ArrayList<Column> cols = new ArrayList<>();
        int type = 0;
        String _name;
        Boolean isKey;
        for (int i = 0; i < getRowDataCount(); i++) {
            if (((String) table.getModel().getValueAt(i, 0)) != null) {
                isKey = true;
            } else {
                isKey = false;
            }
            _name = (String) table.getModel().getValueAt(i, 1);
            _name = _name.toUpperCase();
            if (((String) table.getModel().getValueAt(i, 2)).equals("Text")) {
                type = 1;
            } else {
                type = 0;
            }

            Column col = new Column(_name, type, isKey);

            cols.add(col);
        }
        return cols;
    }

    public String value(int i, int j) {
        return (String) table.getModel().getValueAt(i, j);
    }

    public int getRowDataCount() {
        int countname = 0;
        int counttype = 0;
        for (int i = 0; i < table.getRowCount(); i++) {
            if (((String) table.getModel().getValueAt(i, 1)) != null) {
                countname++;
            }
            if (((String) table.getModel().getValueAt(i, 2)) != null) {
                counttype++;
            }
        }
        if (countname > counttype) {
            return countname;
        }
        return counttype;
    }

    public Boolean InputCorrect() {
        for (int i = 0; i < getRowDataCount(); i++) {
            for (int j = 1; j < 3; j++) {
                if (((String) table.getModel().getValueAt(i, j)) == null) {
                    return false;
                }
            }
        }
        return true;

    }

    private class TabButton extends JButton implements ActionListener {

        public TabButton() {
            int size = 17;
            setPreferredSize(new Dimension(size, size));
            setToolTipText("close this tab");
            //Make the button looks the same for all Laf's
            setUI(new BasicButtonUI());
            //Make it transparent
            setContentAreaFilled(false);
            //No need to be focusable
            setFocusable(false);
            setBorder(BorderFactory.createEtchedBorder());
            setBorderPainted(false);
            //Making nice rollover effect
            //we use the same listener for all buttons

            addMouseListener(buttonMouseListener);
            setRolloverEnabled(true);
            //Close the proper tab by clicking the button
            addActionListener(this);
        }

        public void actionPerformed(ActionEvent e) {
            int i = pane.indexOfTabComponent(TabbedTable.this);
            if (i != -1) {
                pane.remove(i);

            }
        }

        //we don't want to update UI for this button
        public void updateUI() {
        }

        //paint the cross
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            Stroke stroke = g2.getStroke();
            //shift the image for pressed buttons
            if (!getModel().isPressed()) {
                g2.translate(-1, -1);
            }
            g2.setStroke(new BasicStroke(2));
            g.setColor(Color.BLACK);
            if (getModel().isRollover()) {
                g.setColor(Color.MAGENTA);
            }
            int delta = 6;
            g.drawLine(delta, delta, getWidth() - delta - 1, getHeight() - delta - 1);
            g.drawLine(getWidth() - delta - 1, delta, delta, getHeight() - delta - 1);
            //leave the graphics unchanged
            if (!getModel().isPressed()) {
                g.translate(1, 1);
            }
            g2.setStroke(stroke);
        }
    }
    private final static MouseListener buttonMouseListener = new MouseAdapter() {

        public void mouseEntered(MouseEvent e) {
            Component component = e.getComponent();

            if (component instanceof AbstractButton) {
                AbstractButton button = (AbstractButton) component;
                button.setBorderPainted(true);
            }
        }

        public void mouseExited(MouseEvent e) {
            Component component = e.getComponent();
            if (component instanceof AbstractButton) {
                AbstractButton button = (AbstractButton) component;
                button.setBorderPainted(false);
            }
        }
    };
}
